package LineCode.Recruit2017.滴滴出行;

import java.util.*;

/**
 * 某餐馆有n张桌子，每张桌子有一个参数：a 可容纳的最大人数；
 * 有m批客人，每批客人有两个参数:b人数，c预计消费金额。
 * 在不允许拼桌的情况下，请实现一个算法选择其中一部分客人，使得总预计消费金额最大
 *
 * 主要思想：
 * 1.花费能力强的先
 * 2.相同花费人数多的先
 * 3.小桌子先？
 *
 * 自己遇到的坑
 * 1.总金额没用Long
 * 2.占用了桌子后table[i] = 0 (应该是Integer.MIN_VALUE)
 */
public class 餐馆 {

    public static void main(String[] args) {

        //输入
        Scanner sc = new Scanner(System.in);
        Integer n = sc.nextInt();
        Integer m = sc.nextInt();
        Integer table[] = new Integer[n];
        Long income = 0L;
        Integer target = n;
        PriorityQueue<Customer> customers = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            table[i] = sc.nextInt();
        }
        Arrays.sort(table);

        for (int i = 0; i < m; i++) {
            Integer num = sc.nextInt();
            Integer cost = sc.nextInt();
            if (num <= table[n-1])
                customers.add(new Customer(num, cost));
        }

        //处理
        while (!customers.isEmpty()) {
            Customer customer = customers.poll();
            for (int i = 0; i < n; i++) {
                if (customer.num <= table[i]) {
                    income += customer.cost;
                    target--;
                    table[i] = Integer.MIN_VALUE;
                    break;
                }
            }
            if (target <= 0) {
                break;
            }
        }

        //输出
        System.out.println(income);
    }

    private static class Customer implements Comparable<Customer> {
        Integer num;
        Integer cost;

        public Customer(Integer num, Integer cost) {
            this.num = num;
            this.cost = cost;
        }

        @Override
        public int compareTo(Customer o) {
            if (o.cost == this.cost) {
                return o.num - this.num;
            } else {
                return o.cost - this.cost;
            }
        }

    }

}
